import { PlayerState } from './player'

export type Command = {
  id: string;
  seq: number;
  cmd: string;
  target: string;
  value: string;
  extra?: any;
  targetOptions?: string[];
}

export enum CommandRunningStatus {
  Pending,
  Success,
  Failure,
  Warning
}

export type Macro = {
  id: string;
  name: string;
  path: string;
  commands: Command[];
  createdAt?: number;
}

export enum MacroStatus {
  Idle,
  Running,
  Calling,
  Finished,
  Error
}

export type MacroRunningStatus = {
  status: MacroStatus;
  nextIndex: number;
  commandResults: CommandRunningStatus[];
  playerState: PlayerState<Command>;
  interpreterState?: any;
}

export type DoneErrorIds = {
  doneIds: string[];
  errorIds: string[];
  warningIds: string[];
  ids: string[];
}

export function getCommandResults(data: DoneErrorIds): CommandRunningStatus[] {
  const { doneIds, errorIds, warningIds, ids } = data
  const result = [] as CommandRunningStatus[]

  for (let i = 0; i < ids.length; i++) {
    const id = ids[i]
    if (doneIds.includes(id)) {
      result.push(CommandRunningStatus.Success)
    } else if (errorIds.includes(id)) {
      result.push(CommandRunningStatus.Failure)
    } else if (warningIds.includes(id)) {
      result.push(CommandRunningStatus.Warning)
    } else {
      result.push(CommandRunningStatus.Pending)
    }
  }

  return result
}

